package com.plian.Tools.pagedata;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.*;
/**
 * 说明：参数封装Map 创建人：hdu_2017 修改时间：2014年9月20日
 * 
 * @version
 */
public class PageData extends HashMap implements Map {
	Map map = null;
	HttpServletRequest request;
	public PageData(HttpServletRequest request) {//接收
		this.request = request;
		Map properties = request.getParameterMap();
		String[] attrs = (String[]) request.getAttribute("NEEDADD");
		Map returnMap = new HashMap();
		Iterator entries = properties.entrySet().iterator();
		Entry entry;
		String name = "";
		String value = "";
		while (entries.hasNext()) {
			entry = (Entry) entries.next();
			name = (String) entry.getKey();
			Object valueObj = entry.getValue();
			if (null == valueObj) {
				value = "";
			} else if (valueObj instanceof String[]) {
				String[] values = (String[]) valueObj;
				for (int i = 0; i < values.length; i++) {
					value = values[i] + ",";//？value=values[values.length],
				}
				value = value.substring(0, value.length() - 1);//？value=values[values.length]
			} else {
				value = valueObj.toString();
			}
			returnMap.put(name, value);
		}
		if (attrs != null) {//需要插入操作
			for (String attrName : attrs) {
				returnMap.put(attrName, request.getAttribute(attrName));
			}
		}
		map = returnMap;
	}

	public PageData() {
		map = new HashMap();
	}

	@Override
	public Object get(Object key) {
		Object obj = null;
		if (map.get(key) instanceof Object[]) {
			Object[] arr = (Object[]) map.get(key);
			obj = request == null ? arr : (request.getParameter((String) key) == null ? arr : arr[0]);
		} else {
			obj = map.get(key);
		}
		return obj;
	}

	public Date getDate(Object key){
		Object obj = get(key);
		if (obj instanceof Date){
			return (Date)obj;
		}
		if (obj instanceof Timestamp){
			Timestamp timestamp = (Timestamp)obj;
			return new Date(timestamp.getTime());
		}
		return null;
	}

	public String getString(Object key) {
		String str = null;
		try {
			str = (String) get(key);
		} catch (ClassCastException e) {
			str = get(key).toString();
		}
		return str;
	}

	public Long getLong(Object key) {
		Object o = get(key);
		if(o == null){
			return null;
		}
		return Long.parseLong(o + "");
	}

	public Integer getInteger(Object key){
		Object o = get(key);
		if(o == null){
			return null;
		}
		return Integer.parseInt(o + "");
	}


	@SuppressWarnings("unchecked")
	@Override
	public Object put(Object key, Object value) {
		return map.put(key, value);
	}

	@Override
	public Object remove(Object key) {
		return map.remove(key);
	}

	@Override
    public void clear() {
		map.clear();
	}

	@Override
    public boolean containsKey(Object key) {
		// TODO Auto-generated method stub
		return map.containsKey(key);
	}

	@Override
    public boolean containsValue(Object value) {
		// TODO Auto-generated method stub
		return map.containsValue(value);
	}

	@Override
    public Set entrySet() {
		// TODO Auto-generated method stub
		return map.entrySet();
	}

	@Override
    public boolean isEmpty() {
		// TODO Auto-generated method stub
		return map.isEmpty();
	}

	@Override
    public Set keySet() {
		// TODO Auto-generated method stub
		return map.keySet();
	}

	@Override
    @SuppressWarnings("unchecked")
	public void putAll(Map t) {
		// TODO Auto-generated method stub
		map.putAll(t);
	}

	@Override
    public int size() {
		// TODO Auto-generated method stub
		return map.size();
	}

	@Override
    public Collection values() {
		// TODO Auto-generated method stub
		return map.values();
	}

}
